<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>treenode_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_structs_treenode_test.js.html">treenode_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2010 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>goog.provide(&#39;goog.structs.TreeNodeTest&#39;);
<a name="line16"></a>goog.setTestOnly(&#39;goog.structs.TreeNodeTest&#39;);
<a name="line17"></a>
<a name="line18"></a>goog.require(&#39;goog.structs.TreeNode&#39;);
<a name="line19"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line20"></a>
<a name="line21"></a>function testConstructor() {
<a name="line22"></a>  var node = new goog.structs.TreeNode(&#39;key&#39;, &#39;value&#39;);
<a name="line23"></a>  assertEquals(&#39;key&#39;, &#39;key&#39;, node.getKey());
<a name="line24"></a>  assertEquals(&#39;value&#39;, &#39;value&#39;, node.getValue());
<a name="line25"></a>  assertNull(&#39;parent&#39;, node.getParent());
<a name="line26"></a>  assertArrayEquals(&#39;children&#39;, [], node.getChildren());
<a name="line27"></a>  assertTrue(&#39;leaf&#39;, node.isLeaf());
<a name="line28"></a>}
<a name="line29"></a>
<a name="line30"></a>function testClone() {
<a name="line31"></a>  var node1 = new goog.structs.TreeNode(1, &#39;1&#39;);
<a name="line32"></a>  var node2 = new goog.structs.TreeNode(2, &#39;2&#39;);
<a name="line33"></a>  var node3 = new goog.structs.TreeNode(3, &#39;3&#39;);
<a name="line34"></a>  node1.addChild(node2);
<a name="line35"></a>  node2.addChild(node3);
<a name="line36"></a>
<a name="line37"></a>  var clone = node2.clone();
<a name="line38"></a>  assertEquals(&#39;key&#39;, 2, clone.getKey());
<a name="line39"></a>  assertEquals(&#39;value&#39;, &#39;2&#39;, clone.getValue());
<a name="line40"></a>  assertNull(&#39;parent&#39;, clone.getParent());
<a name="line41"></a>  assertArrayEquals(&#39;children&#39;, [], clone.getChildren());
<a name="line42"></a>}
<a name="line43"></a>
<a name="line44"></a>function testDeepClone() {
<a name="line45"></a>  var node1 = new goog.structs.TreeNode(1, &#39;1&#39;);
<a name="line46"></a>  var node2 = new goog.structs.TreeNode(2, &#39;2&#39;);
<a name="line47"></a>  var node3 = new goog.structs.TreeNode(3, &#39;3&#39;);
<a name="line48"></a>  node1.addChild(node2);
<a name="line49"></a>  node2.addChild(node3);
<a name="line50"></a>
<a name="line51"></a>  var clone = node2.deepClone();
<a name="line52"></a>  assertEquals(&#39;key&#39;, 2, clone.getKey());
<a name="line53"></a>  assertEquals(&#39;value&#39;, &#39;2&#39;, clone.getValue());
<a name="line54"></a>  assertNull(&#39;parent&#39;, clone.getParent());
<a name="line55"></a>  assertEquals(&#39;number of children&#39;, 1, clone.getChildren().length);
<a name="line56"></a>  assertEquals(&#39;first child key&#39;, 3, clone.getChildAt(0).getKey());
<a name="line57"></a>  assertNotEquals(&#39;first child has been cloned&#39;, node3, clone.getChildAt(0));
<a name="line58"></a>}
<a name="line59"></a>
<a name="line60"></a>function testGetParent() {
<a name="line61"></a>  var node1 = new goog.structs.TreeNode(1, null);
<a name="line62"></a>  var node2 = new goog.structs.TreeNode(2, null);
<a name="line63"></a>  node1.addChild(node2);
<a name="line64"></a>  assertEquals(&#39;parent&#39;, node1, node2.getParent());
<a name="line65"></a>  assertNull(&#39;orphan&#39;, node1.getParent());
<a name="line66"></a>}
<a name="line67"></a>
<a name="line68"></a>function testIsLeaf() {
<a name="line69"></a>  var node1 = new goog.structs.TreeNode(1, null);
<a name="line70"></a>  var node2 = new goog.structs.TreeNode(2, null);
<a name="line71"></a>  node1.addChild(node2);
<a name="line72"></a>  assertFalse(&#39;not leaf&#39;, node1.isLeaf());
<a name="line73"></a>  assertTrue(&#39;leaf&#39;, node2.isLeaf());
<a name="line74"></a>}
<a name="line75"></a>
<a name="line76"></a>function testIsLastChild() {
<a name="line77"></a>  var node1 = new goog.structs.TreeNode(1, &#39;1&#39;);
<a name="line78"></a>  var node2 = new goog.structs.TreeNode(2, &#39;2&#39;);
<a name="line79"></a>  var node3 = new goog.structs.TreeNode(3, &#39;3&#39;);
<a name="line80"></a>  node1.addChild(node2);
<a name="line81"></a>  node1.addChild(node3);
<a name="line82"></a>  assertFalse(&#39;root&#39;, node1.isLastChild());
<a name="line83"></a>  assertFalse(&#39;first child out of the two&#39;, node2.isLastChild());
<a name="line84"></a>  assertTrue(&#39;second child out of the two&#39;, node3.isLastChild());
<a name="line85"></a>}
<a name="line86"></a>
<a name="line87"></a>function testGetChildren() {
<a name="line88"></a>  var node1 = new goog.structs.TreeNode(1, null);
<a name="line89"></a>  var node2 = new goog.structs.TreeNode(2, null);
<a name="line90"></a>  node1.addChild(node2);
<a name="line91"></a>  assertArrayEquals(&#39;1 child&#39;, [node2], node1.getChildren());
<a name="line92"></a>  assertArrayEquals(&#39;no children&#39;, [], node2.getChildren());
<a name="line93"></a>}
<a name="line94"></a>
<a name="line95"></a>function testGetChildAt() {
<a name="line96"></a>  var node1 = new goog.structs.TreeNode(1, null);
<a name="line97"></a>  var node2 = new goog.structs.TreeNode(2, null);
<a name="line98"></a>  node1.addChild(node2);
<a name="line99"></a>  assertNull(&#39;index too low&#39;, node1.getChildAt(-1));
<a name="line100"></a>  assertEquals(&#39;first child&#39;, node2, node1.getChildAt(0));
<a name="line101"></a>  assertNull(&#39;index too high&#39;, node1.getChildAt(1));
<a name="line102"></a>  assertNull(&#39;no children&#39;, node2.getChildAt(0));
<a name="line103"></a>}
<a name="line104"></a>
<a name="line105"></a>function testGetChildCount() {
<a name="line106"></a>  var node1 = new goog.structs.TreeNode(1, null);
<a name="line107"></a>  var node2 = new goog.structs.TreeNode(2, null);
<a name="line108"></a>  node1.addChild(node2);
<a name="line109"></a>  assertEquals(&#39;child count of root node&#39;, 1, node1.getChildCount());
<a name="line110"></a>  assertEquals(&#39;child count of leaf node&#39;, 0, node2.getChildCount());
<a name="line111"></a>}
<a name="line112"></a>
<a name="line113"></a>function testGetDepth() {
<a name="line114"></a>  var node1 = new goog.structs.TreeNode(1, null);
<a name="line115"></a>  var node2 = new goog.structs.TreeNode(2, null);
<a name="line116"></a>  var node3 = new goog.structs.TreeNode(3, null);
<a name="line117"></a>  node1.addChild(node2);
<a name="line118"></a>  node2.addChild(node3);
<a name="line119"></a>  assertEquals(&#39;no parent&#39;, 0, node1.getDepth());
<a name="line120"></a>  assertEquals(&#39;1 ancestor&#39;, 1, node2.getDepth());
<a name="line121"></a>  assertEquals(&#39;2 ancestors&#39;, 2, node3.getDepth());
<a name="line122"></a>}
<a name="line123"></a>
<a name="line124"></a>function testGetAncestors() {
<a name="line125"></a>  var node1 = new goog.structs.TreeNode(1, null);
<a name="line126"></a>  var node2 = new goog.structs.TreeNode(2, null);
<a name="line127"></a>  var node3 = new goog.structs.TreeNode(3, null);
<a name="line128"></a>  node1.addChild(node2);
<a name="line129"></a>  node2.addChild(node3);
<a name="line130"></a>  assertArrayEquals(&#39;no ancestors&#39;, [], node1.getAncestors());
<a name="line131"></a>  assertArrayEquals(&#39;2 ancestors&#39;, [node2, node1], node3.getAncestors());
<a name="line132"></a>}
<a name="line133"></a>
<a name="line134"></a>function testGetRoot() {
<a name="line135"></a>  var node1 = new goog.structs.TreeNode(1, null);
<a name="line136"></a>  var node2 = new goog.structs.TreeNode(2, null);
<a name="line137"></a>  node1.addChild(node2);
<a name="line138"></a>  assertEquals(&#39;no ancestors&#39;, node1, node1.getRoot());
<a name="line139"></a>  assertEquals(&#39;has ancestors&#39;, node1, node2.getRoot());
<a name="line140"></a>}
<a name="line141"></a>
<a name="line142"></a>function testGetSubtreeKeys() {
<a name="line143"></a>  var root = new goog.structs.TreeNode(&#39;root&#39;, null);
<a name="line144"></a>  var child1 = new goog.structs.TreeNode(&#39;child1&#39;, null);
<a name="line145"></a>  var child2 = new goog.structs.TreeNode(&#39;child2&#39;, null);
<a name="line146"></a>  var grandchild = new goog.structs.TreeNode(&#39;grandchild&#39;, null);
<a name="line147"></a>  root.addChild(child1);
<a name="line148"></a>  root.addChild(child2);
<a name="line149"></a>  child1.addChild(grandchild);
<a name="line150"></a>  assertArrayEquals(&#39;node hierarchy&#39;, [&#39;child1&#39;, [&#39;grandchild&#39;], &#39;child2&#39;],
<a name="line151"></a>      root.getSubtreeKeys());
<a name="line152"></a>}
<a name="line153"></a>
<a name="line154"></a>function testContains() {
<a name="line155"></a>  var node1 = new goog.structs.TreeNode(1, null);
<a name="line156"></a>  var node2 = new goog.structs.TreeNode(2, null);
<a name="line157"></a>  var node3 = new goog.structs.TreeNode(3, null);
<a name="line158"></a>  var node4 = new goog.structs.TreeNode(4, null);
<a name="line159"></a>  node1.addChild(node2);
<a name="line160"></a>  node2.addChild(node3);
<a name="line161"></a>  node2.addChild(node4);
<a name="line162"></a>
<a name="line163"></a>  assertTrue(&#39;parent&#39;, node1.contains(node2));
<a name="line164"></a>  assertTrue(&#39;grandparent&#39;, node1.contains(node3));
<a name="line165"></a>  assertFalse(&#39;child&#39;, node2.contains(node1));
<a name="line166"></a>  assertFalse(&#39;grandchild&#39;, node3.contains(node1));
<a name="line167"></a>  assertFalse(&#39;sibling&#39;, node3.contains(node4));
<a name="line168"></a>}
<a name="line169"></a>
<a name="line170"></a>function testFindCommonAncestor() {
<a name="line171"></a>  var findCommonAncestor = goog.structs.TreeNode.findCommonAncestor;
<a name="line172"></a>  var node1 = new goog.structs.TreeNode(1, null);
<a name="line173"></a>  var node2 = new goog.structs.TreeNode(2, null);
<a name="line174"></a>  var node3 = new goog.structs.TreeNode(3, null);
<a name="line175"></a>  var node4 = new goog.structs.TreeNode(4, null);
<a name="line176"></a>  var node5 = new goog.structs.TreeNode(5, null);
<a name="line177"></a>  node1.addChild(node2);
<a name="line178"></a>  node2.addChild(node3);
<a name="line179"></a>  node1.addChild(node4);
<a name="line180"></a>
<a name="line181"></a>  assertNull(&#39;0 nodes&#39;, findCommonAncestor());
<a name="line182"></a>  assertEquals(&#39;1 node&#39;, node2, findCommonAncestor(node2));
<a name="line183"></a>  assertEquals(&#39;same nodes&#39;, node3, findCommonAncestor(node3, node3));
<a name="line184"></a>  assertEquals(&#39;node and child node&#39;, node2, findCommonAncestor(node2, node3));
<a name="line185"></a>  assertEquals(&#39;node and parent node&#39;, node1, findCommonAncestor(node2, node1));
<a name="line186"></a>  assertEquals(&#39;siblings&#39;, node1, findCommonAncestor(node2, node4));
<a name="line187"></a>  assertEquals(&#39;all nodes&#39;, node1,
<a name="line188"></a>      findCommonAncestor(node2, node3, node4, node1));
<a name="line189"></a>  assertNull(&#39;disconnected nodes&#39;, findCommonAncestor(node3, node5));
<a name="line190"></a>}
<a name="line191"></a>
<a name="line192"></a>function testGetNodeByKey() {
<a name="line193"></a>  var node1 = new goog.structs.TreeNode(1, null);
<a name="line194"></a>  var node2 = new goog.structs.TreeNode(2, null);
<a name="line195"></a>  var node3 = new goog.structs.TreeNode(3, null);
<a name="line196"></a>  var node4 = new goog.structs.TreeNode(4, null);
<a name="line197"></a>  var node5 = new goog.structs.TreeNode(2, null);
<a name="line198"></a>  var node6 = new goog.structs.TreeNode(3, null);
<a name="line199"></a>  node1.addChild(node2);
<a name="line200"></a>  node2.addChild(node3);
<a name="line201"></a>  node1.addChild(node4);
<a name="line202"></a>  node4.addChild(node5);
<a name="line203"></a>  node1.addChild(node6);
<a name="line204"></a>
<a name="line205"></a>  assertEquals(&#39;root&#39;, node1, node1.getNodeByKey(1));
<a name="line206"></a>  assertEquals(&#39;child with unique key&#39;, node5, node4.getNodeByKey(2));
<a name="line207"></a>  assertEquals(&#39;child with duplicate keys&#39;, node2, node1.getNodeByKey(2));
<a name="line208"></a>  assertEquals(&#39;grandchild with duplicate keys&#39;, node3, node1.getNodeByKey(3));
<a name="line209"></a>  assertNull(&#39;disconnected&#39;, node2.getNodeByKey(4));
<a name="line210"></a>  assertNull(&#39;missing&#39;, node1.getNodeByKey(5));
<a name="line211"></a>}
<a name="line212"></a>
<a name="line213"></a>function testForEachChild() {
<a name="line214"></a>  var node1 = new goog.structs.TreeNode(1, null);
<a name="line215"></a>  var node2 = new goog.structs.TreeNode(2, null);
<a name="line216"></a>  var node3 = new goog.structs.TreeNode(3, null);
<a name="line217"></a>  node1.addChild(node2);
<a name="line218"></a>  node1.addChild(node3);
<a name="line219"></a>
<a name="line220"></a>  var thisContext = {};
<a name="line221"></a>  var visitedNodes = [];
<a name="line222"></a>  var indices = [];
<a name="line223"></a>  node1.forEachChild(function(node, index, children) {
<a name="line224"></a>    assertEquals(&#39;value of this&#39;, thisContext, this);
<a name="line225"></a>    visitedNodes.push(node);
<a name="line226"></a>    indices.push(index);
<a name="line227"></a>    assertArrayEquals(&#39;children argument&#39;, [node2, node3], children);
<a name="line228"></a>  }, thisContext);
<a name="line229"></a>  assertArrayEquals(&#39;visited nodes&#39;, [node2, node3], visitedNodes);
<a name="line230"></a>  assertArrayEquals(&#39;index of visited nodes&#39;, [0, 1], indices);
<a name="line231"></a>}
<a name="line232"></a>
<a name="line233"></a>function testForEachDescendant() {
<a name="line234"></a>  var node1 = new goog.structs.TreeNode(1, null);
<a name="line235"></a>  var node2 = new goog.structs.TreeNode(2, null);
<a name="line236"></a>  var node3 = new goog.structs.TreeNode(3, null);
<a name="line237"></a>  var node4 = new goog.structs.TreeNode(4, null);
<a name="line238"></a>  node1.addChild(node2);
<a name="line239"></a>  node2.addChild(node3);
<a name="line240"></a>  node2.addChild(node4);
<a name="line241"></a>
<a name="line242"></a>  var thisContext = {};
<a name="line243"></a>  var visitedNodes = [];
<a name="line244"></a>  node1.forEachDescendant(function(node) {
<a name="line245"></a>    assertEquals(&#39;value of this&#39;, thisContext, this);
<a name="line246"></a>    visitedNodes.push(node);
<a name="line247"></a>  }, thisContext);
<a name="line248"></a>  assertArrayEquals(&#39;visited nodes&#39;, [node2, node3, node4], visitedNodes);
<a name="line249"></a>}
<a name="line250"></a>
<a name="line251"></a>function testTraverse() {
<a name="line252"></a>  var node1 = new goog.structs.TreeNode(1, null);
<a name="line253"></a>  var node2 = new goog.structs.TreeNode(2, null);
<a name="line254"></a>  var node3 = new goog.structs.TreeNode(3, null);
<a name="line255"></a>  var node4 = new goog.structs.TreeNode(4, null);
<a name="line256"></a>  node1.addChild(node2);
<a name="line257"></a>  node2.addChild(node3);
<a name="line258"></a>  node2.addChild(node4);
<a name="line259"></a>
<a name="line260"></a>  var thisContext = {};
<a name="line261"></a>  var visitedNodes = [];
<a name="line262"></a>  node1.traverse(function(node) {
<a name="line263"></a>    assertEquals(&#39;value of this&#39;, thisContext, this);
<a name="line264"></a>    visitedNodes.push(node);
<a name="line265"></a>  }, thisContext);
<a name="line266"></a>  assertArrayEquals(&#39;callback returns nothing =&gt; all nodes are visited&#39;,
<a name="line267"></a>      [node1, node2, node3, node4], visitedNodes);
<a name="line268"></a>
<a name="line269"></a>  visitedNodes = [];
<a name="line270"></a>  node1.traverse(function(node) {
<a name="line271"></a>    visitedNodes.push(node);
<a name="line272"></a>    return node != node2;  // Cut off at node2.
<a name="line273"></a>  });
<a name="line274"></a>  assertArrayEquals(&#39;children of node2 are skipped&#39;,
<a name="line275"></a>      [node1, node2], visitedNodes);
<a name="line276"></a>}
<a name="line277"></a>
<a name="line278"></a>function testAddChild() {
<a name="line279"></a>  var node1 = new goog.structs.TreeNode(1, null);
<a name="line280"></a>  var node2 = new goog.structs.TreeNode(2, null);
<a name="line281"></a>  var node3 = new goog.structs.TreeNode(3, null);
<a name="line282"></a>  assertArrayEquals(&#39;0 children&#39;, [], node1.getChildren());
<a name="line283"></a>  node1.addChild(node2);
<a name="line284"></a>  assertArrayEquals(&#39;1 child&#39;, [node2], node1.getChildren());
<a name="line285"></a>  assertEquals(&#39;parent is set&#39;, node1, node2.getParent());
<a name="line286"></a>  node1.addChild(node3);
<a name="line287"></a>  assertArrayEquals(&#39;2 children&#39;, [node2, node3], node1.getChildren());
<a name="line288"></a>}
<a name="line289"></a>
<a name="line290"></a>function testAddChildAt() {
<a name="line291"></a>  var node1 = new goog.structs.TreeNode(1, null);
<a name="line292"></a>  var node2 = new goog.structs.TreeNode(2, null);
<a name="line293"></a>  var node3 = new goog.structs.TreeNode(3, null);
<a name="line294"></a>  var node4 = new goog.structs.TreeNode(4, null);
<a name="line295"></a>  var node5 = new goog.structs.TreeNode(5, null);
<a name="line296"></a>  node1.addChildAt(node2, 0);
<a name="line297"></a>  assertArrayEquals(&#39;add first child&#39;, [node2], node1.getChildren());
<a name="line298"></a>  assertEquals(&#39;parent is set&#39;, node1, node2.getParent());
<a name="line299"></a>  node1.addChildAt(node3, 0);
<a name="line300"></a>  assertArrayEquals(&#39;add to the front&#39;, [node3, node2],
<a name="line301"></a>      node1.getChildren());
<a name="line302"></a>  node1.addChildAt(node4, 1);
<a name="line303"></a>  assertArrayEquals(&#39;add to the middle&#39;, [node3, node4, node2],
<a name="line304"></a>      node1.getChildren());
<a name="line305"></a>  node1.addChildAt(node5, 3);
<a name="line306"></a>  assertArrayEquals(&#39;add to the end&#39;, [node3, node4, node2, node5],
<a name="line307"></a>      node1.getChildren());
<a name="line308"></a>}
<a name="line309"></a>
<a name="line310"></a>function testReplaceChildAt() {
<a name="line311"></a>  var root = new goog.structs.TreeNode(0, null);
<a name="line312"></a>  var node1 = new goog.structs.TreeNode(1, null);
<a name="line313"></a>  root.addChild(node1);
<a name="line314"></a>
<a name="line315"></a>  var node2 = new goog.structs.TreeNode(2, null);
<a name="line316"></a>  assertEquals(&#39;original node&#39;, node1, root.replaceChildAt(node2, 0));
<a name="line317"></a>  assertEquals(&#39;parent is set&#39;, root, node2.getParent());
<a name="line318"></a>  assertArrayEquals(&#39;children are updated&#39;, [node2], root.getChildren());
<a name="line319"></a>  assertNull(&#39;old child node is detached&#39;, node1.getParent());
<a name="line320"></a>}
<a name="line321"></a>
<a name="line322"></a>function testReplaceChild() {
<a name="line323"></a>  var root = new goog.structs.TreeNode(0, null);
<a name="line324"></a>  var node1 = new goog.structs.TreeNode(1, null);
<a name="line325"></a>  root.addChild(node1);
<a name="line326"></a>
<a name="line327"></a>  var node2 = new goog.structs.TreeNode(2, null);
<a name="line328"></a>  assertEquals(&#39;original node&#39;, node1, root.replaceChild(node2, node1));
<a name="line329"></a>  assertEquals(&#39;parent is set&#39;, root, node2.getParent());
<a name="line330"></a>  assertArrayEquals(&#39;children are updated&#39;, [node2], root.getChildren());
<a name="line331"></a>  assertNull(&#39;old child node is detached&#39;, node1.getParent());
<a name="line332"></a>}
<a name="line333"></a>
<a name="line334"></a>function testRemoveChildAt() {
<a name="line335"></a>  var node1 = new goog.structs.TreeNode(1, null);
<a name="line336"></a>  var node2 = new goog.structs.TreeNode(2, null);
<a name="line337"></a>  var node3 = new goog.structs.TreeNode(3, null);
<a name="line338"></a>  node1.addChild(node2);
<a name="line339"></a>  node1.addChild(node3);
<a name="line340"></a>
<a name="line341"></a>  assertNull(&#39;index too low&#39;, node1.removeChildAt(-1));
<a name="line342"></a>  assertNull(&#39;index too high&#39;, node1.removeChildAt(2));
<a name="line343"></a>  assertArrayEquals(&#39;node1 is intact&#39;, [node2, node3], node1.getChildren());
<a name="line344"></a>
<a name="line345"></a>  assertEquals(&#39;remove first child&#39;, node2, node1.removeChildAt(0));
<a name="line346"></a>  assertArrayEquals(&#39;remove from the front&#39;, [node3], node1.getChildren());
<a name="line347"></a>  assertNull(&#39;parent is unset&#39;, node2.getParent());
<a name="line348"></a>
<a name="line349"></a>  assertEquals(&#39;remove last child&#39;, node3, node1.removeChildAt(0));
<a name="line350"></a>  assertArrayEquals(&#39;remove last child&#39;, [], node1.getChildren());
<a name="line351"></a>  assertTrue(&#39;node1 became leaf&#39;, node1.isLeaf());
<a name="line352"></a>}
<a name="line353"></a>
<a name="line354"></a>function testRemoveChild() {
<a name="line355"></a>  var node1 = new goog.structs.TreeNode(1, null);
<a name="line356"></a>  var node2 = new goog.structs.TreeNode(2, null);
<a name="line357"></a>  var node3 = new goog.structs.TreeNode(3, null);
<a name="line358"></a>  node1.addChild(node2);
<a name="line359"></a>  node1.addChild(node3);
<a name="line360"></a>
<a name="line361"></a>  assertNull(&#39;remove null&#39;, node1.removeChild(null));
<a name="line362"></a>  assertNull(&#39;remove non-child&#39;, node1.removeChild(node1));
<a name="line363"></a>  assertArrayEquals(&#39;node1 is intact&#39;, [node2, node3], node1.getChildren());
<a name="line364"></a>
<a name="line365"></a>  assertEquals(&#39;remove node3, return value&#39;, node3, node1.removeChild(node3));
<a name="line366"></a>  assertArrayEquals(&#39;node is removed&#39;, [node2], node1.getChildren());
<a name="line367"></a>}
<a name="line368"></a>
<a name="line369"></a>function testRemoveChildren() {
<a name="line370"></a>  var node1 = new goog.structs.TreeNode(1, null);
<a name="line371"></a>  var node2 = new goog.structs.TreeNode(2, null);
<a name="line372"></a>  var node3 = new goog.structs.TreeNode(3, null);
<a name="line373"></a>  node1.addChild(node2);
<a name="line374"></a>  node1.addChild(node3);
<a name="line375"></a>
<a name="line376"></a>  node2.removeChildren();
<a name="line377"></a>  assertArrayEquals(&#39;removing a leaf node\&#39;s children has no effect&#39;, [],
<a name="line378"></a>      node2.getChildren());
<a name="line379"></a>  assertEquals(&#39;node still has parent&#39;, node1, node2.getParent());
<a name="line380"></a>
<a name="line381"></a>  node1.removeChildren();
<a name="line382"></a>  assertArrayEquals(&#39;children have been removed&#39;, [], node1.getChildren());
<a name="line383"></a>  assertNull(&#39;children\&#39;s parents have been unset&#39;, node2.getParent());
<a name="line384"></a>}
</pre>


</body>
</html>
